from fastapi import APIRouter, Query,UploadFile
from tortoise.expressions import Q
from app.controllers.marketing.draw_rule  import draw_rule_controller
from app.models import CouponsRole, Goods
from app.schemas.base import Success, SuccessExtra,Fail
from app.schemas.marketing import DrawRoleUpdate, DrawRoleCreate
from app.utils.upload import upload_image,upload_file
from app.settings import settings

draw_rule_router = APIRouter()
model_name = "抽奖商品规则"


@draw_rule_router.get("/list", summary=f"查看{model_name}列表")
async def do_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query(None, description=f"名称"),
):
    q = Q()
    if name:
        q &= Q(name__contains=name)
    total, data = await draw_rule_controller.do_list(page=page, page_size=page_size, search=q)

    coupons_rules = await CouponsRole.all()
    coupons_rule_dict = {coupons_rule.id: coupons_rule.name for coupons_rule in coupons_rules}

    goods = await Goods.all()
    goods_dict = {good.id: good.name for good in goods}

    for item in data:
        item["type_name"] = settings.DRAWROLE_TYPE[item["type"]]
        if item["type"]==1:
            item["goods_name"] = f"{item["type_id"]}折"
        elif item["type"]==2:
            item["goods_name"] = coupons_rule_dict.get(item["type_id"],"")
        elif item["type"]==3:
            item["goods_name"] = goods_dict[item["type_id"]]
        elif item["type"]==4:
            item["goods_name"] = f"{item["type_id"]}积分"
        elif item["type"]==0:
            item["goods_name"] = "无"

    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@draw_rule_router.get("/get", summary=f"查看{model_name}")
async def do_get(
        data_id: int = Query(..., description="ID"),
):
    data = await draw_rule_controller.do_detail(data_id=data_id)
    return Success(data=data)

@draw_rule_router.post("/create", summary=f"创建{model_name}")
async def do_create(
        category_in: DrawRoleCreate,
):
    await draw_rule_controller.do_create(obj_in=category_in)
    return Success(msg="创建成功")

@draw_rule_router.post("/update", summary=f"更新{model_name}")
async def do_update(
        category_in: DrawRoleUpdate,
):
    await draw_rule_controller.do_update(obj_in=category_in)
    return Success(msg="更新成功")

@draw_rule_router.delete("/delete", summary=f"删除{model_name}")
async def do_delete(
        data_id: int = Query(..., description="ID"),
):
    await draw_rule_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")

@draw_rule_router.get("/rules", summary="获取规则下拉")
async def list_rules():
    level_objs = await draw_rule_controller.level_list()
    data = []
    for level in level_objs:
        if level:
            data.append({"label": level_objs[level], "value": level})
    return Success(data=data)

@draw_rule_router.get("/type", summary="获取奖品类型")
async def type_rules():
    level_objs = settings.DRAWROLE_TYPE
    data = []
    key = 0
    for level in level_objs:
        if level:
            data.append({"label": level, "value": key})
        key += 1
    return Success(data=data)

@draw_rule_router.post("/file", summary="上传图片")
async def do_file(file: UploadFile):
    url = await upload_image(file,"marketing/draw_rule")
    if not url:
        return Fail(code=400, msg="图片格式错误")
    data = {"url": url}
    return Success(data=data)
